package com.jxs.android.weixi;

import java.io.IOException;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.xmlpull.v1.XmlPullParserException;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.jxs.android.soap.BuWebService;
import com.jxs.android.soap.IWebService;
import com.jxs.android.util.StringUtils;
import com.jxs.android.weiget.BuAlertDailog;
import com.jxs.android.weiget.BuLoadingDailog;
import com.jxs.android.weiget.TopBar;
import com.telecom.weixi.R;

@SuppressLint("HandlerLeak")
public class MyWork_Customer_Detain extends AbstractActivity {

	private String title, name_label, name_value, detain_label, detain_value;

	private String cust_id;
	private EditText exec_noteEditText;
	private TextView cst_name_label, cst_name_value, detain_info_label, detain_info_value;

	private Button submit;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		if (null != getIntent() && null != getIntent().getExtras()) {
			Bundle bundle = getIntent().getExtras();
			title = bundle.getString("title");
			cust_id = bundle.getString("cust_id");

			name_label = bundle.getString("name_label");
			name_value = bundle.getString("name_value");
			detain_label = bundle.getString("detain_label");
			detain_value = bundle.getString("detain_value");
		}
		initTopBar(savedInstanceState, R.layout.bu_my_work_common_detain);
		exec_noteEditText = (EditText) findViewById(R.id.exec_note);
		submit = (Button) findViewById(R.id.sumbit);
		submit.setOnClickListener(submitClickListener);

		cst_name_label = (TextView) findViewById(R.id.cst_name_label);
		cst_name_value = (TextView) findViewById(R.id.cst_name_value);
		detain_info_label = (TextView) findViewById(R.id.detain_info_label);
		detain_info_value = (TextView) findViewById(R.id.detain_info_value);

		cst_name_label.setText(name_label);
		cst_name_value.setText(name_value);
		detain_info_label.setText(detain_label);
		detain_info_value.setText(detain_value);

	}

	private View.OnClickListener submitClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			if (StringUtils.isEmpety(exec_noteEditText.getText().toString())) {
				exec_noteEditText.setText(R.string.exec_note_hit);
			}
			submit(exec_noteEditText.getText().toString());
		}
	};

	@Override
	public void topBarLogic(TopBar topBar) {
		topBar.setTitle(title);
		topBar.hindBtn();
		topBarReturn(topBar);
	}

	private void submit(final String exec_note) {
		progressDialog = new BuLoadingDailog(this);
		progressDialog.setCancelable(true);
		progressDialog.showLoading(R.string.prompt_loading);
		new Thread(new Runnable() {

			@Override
			public void run() {
				String wsdl_url = WeixiApplication.WSDL_URL;// wsdl
				String namespace = WeixiApplication.WSDL_NAME_SPACE;// namespace
				String methodName = "doExecFeeBackCust";// 要调用的方法名称
				SoapObject request = new SoapObject(namespace, methodName);

				request.addProperty("ticket", WeixiApplication.getUserInfo().getTicket());
				request.addProperty("cust_id", cust_id);
				request.addProperty("exec_note", exec_note);
				BuWebService webService = new BuWebService(wsdl_url, request);
				webService.setDoNet(false);
				webService.requestWSDL(new IWebService() {
					@Override
					public void logic(SoapSerializationEnvelope envelope) throws IOException, XmlPullParserException {
						paser(envelope.getResponse().toString());
					}

					@Override
					public void error(String message, Exception e) {
						Message msg = new Message();
						msg.what = ERROR_MSG;
						msg.obj = message;
						handler.sendMessage(msg);
					}
				});
			}
		}).start();
	}

	private void paser(String stauts) {
		if ("-1".equals(stauts)) {
			Message msg = new Message();
			msg.what = LOGIN_ON;
			msg.obj = "登录已过期，请重新登录";
			handler.sendMessage(msg);
		} else if ("-2".equals(stauts)) {
			Message msg = new Message();
			msg.what = ERROR_MSG;
			msg.obj = "服务端异常";
			handler.sendMessage(msg);
		} else if ("-3".equals(stauts)) {
			Message msg = new Message();
			msg.what = ERROR_MSG;
			msg.obj = "输入参数无效!";
			handler.sendMessage(msg);
		} else if ("0".equals(stauts)) {
			handler.sendEmptyMessage(SUCCESS);
		}
	}

	private int LOGIN_ON = 0x110;
	private int ERROR_MSG = 0x112;
	private int SUCCESS = 0x113;

	@SuppressLint({ "HandlerLeak", "HandlerLeak" })
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (null != progressDialog && progressDialog.isShowing()) {
				progressDialog.cancel();
			}
			if (msg.what == LOGIN_ON) {
				errorMsgToLogin(msg.obj.toString());
			} else if (msg.what == ERROR_MSG) {
				errorMsgPop(msg.obj.toString());
			} else if (msg.what == SUCCESS) {
				errorMsgPop("处理成功!", new BuAlertDailog.IPhoneAlertDailog() {

					@Override
					public void okLogic(Dialog dialog) {
						setResult(RESULT_OK, getIntent());
						finish();
					}

					@Override
					public String getOkText(Context context) {
						return null;
					}
				});
			}
		}

	};

}
